<template>
    <div>
        <h1>添加</h1>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td align="right">账号</td>
                    <td><input type="text" v-model="info.AdminName"></td>
                </tr>
                <tr>
                    <td align="right">密码</td>
                    <td><input type="text" v-model="info.AdminPwd"></td>
                </tr>
                <tr>
                    <td align="right">姓名</td>
                    <td><input type="text" v-model="info.UserName"></td>
                </tr>
                <tr>
                    <td align="right">手机号</td>
                    <td><input type="text" v-model="info.Tel"></td>
                </tr>
                <tr>
                    <td align="right">图片</td>
                    <td><input type="file" @change="Img">
                        <img :src="info.Pic" width="100" height="100" v-if="info.Pic != ''">
                    </td>
                </tr>
                <tr>
                    <td align="right">角色</td>
                    <td>
                        <select v-model="info.RoleId">
                            <option value="">请选择</option>
                            <option :value="itme.Id" v-for="itme in lisr.a" :key="itme.Id"> {{ itme.RoleName }}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align="right"></td>
                    <td><input type="button" value="添加" @click="ok"></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios';
import { useRouter } from 'vue-router';
const rouder = useRouter();
let info: any = reactive({
    AdminName: '',
    AdminPwd: '',
    UserName: '',
    Tel: '',
    Pic: '',
    RoleId: '',

})
let lisr: any = reactive({
    a: []
})
onMounted(() => {
    getlisr();
})
const getlisr = () => {
    axios({
        url: '/api/Memder/ShowR',
        method: 'get'
    })
        .then((res) => {
            console.log(res);
            lisr.a = res.data;
        })
        .catch((err) => {
            console.log(err);

        })

}


const Img = (e: any) => {
    let obj = e.target.files[0];
    let f = new FormData();
    f.append('file', obj)
    axios({
        url: '/api/Memder/Imgs',
        method: 'post',
        data: f
    })
        .then((res) => {
            console.log(res);
            if (res.data == '你上传的不是图片"') {
                alert('你上传的不是图片');
                return;
            }
            if ((res.data == '大于2M')) {
                alert("大于2M");
                return;
            }
            info.Pic = res.data
        })
        .catch((err) => {
            console.log(err);

        })
}




const ok = () => {
    axios({
        url: '/api/Memder/AdminAdd',
        method: 'post',
        data: info
    })
        .then((res) => {
            console.log(res);
            if (res.data == -1) {
                alert('账号重复');
                return;
            }
            if (res.data > 0) {
                alert('添加成功');
                rouder.push('/showhy')
                return;
            }
            else {
                alert('添加失败');
                return;
            }
        })
        .catch((err) => {
            console.log(err);

        })
}
</script>

<style scoped></style>